package com.zhiyou100.action;

import com.zhiyou100.exception.MyException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yang
 * @className MyExceptionHandler
 * @packageName com.zhiyou100.action
 * @Description TODO 处理异常的 handler
 * @date 2020-08-25 14:16
 */
@Component
public class MyExceptionHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

        if (ex instanceof MyException) {
            final ModelAndView mv = new ModelAndView();
            mv.addObject("errorMessage", ex.getMessage());
            mv.setViewName("test05_exception/error");
            return mv;
        }
        return null;
    }
}
